package breeze.lepei.der.logo_view.view;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import breeze.lepei.der.BaseView;
import breeze.lepei.der.R;
import breeze.lepei.der.loginview.view.LoginView;
import breeze.lepei.der.logo_view.presenter.ILogoPresenter;
import breeze.lepei.der.logo_view.presenter.LogoPresenter;
import breeze.lepei.der.usersettings.UserSettings;
import breeze.lepei.der.utils.AppTools;
import breeze.lepei.der.utils.GradientDrawableUtil;
import breeze.lepei.der.utils.MDialog;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.graphics.Color;
import android.text.Spanned;
import android.widget.ProgressBar;
import breeze.lepei.der.main_view.view.MainView;
import android.net.Uri;

public class LogoView extends BaseView implements ILogoView,OnClickListener {

	/**
	*@author BREEZE
	*@description 显示版本更新
	*@param 下载链接
	*/
	@Override
	public void showNewVersion(final int versionCode, final String downloadLink,final String updateInfo) {
		runOnUiThread(new Runnable(){public void run() {
					mLog.log("版本更新","版本号："+String.valueOf(versionCode)+"；下载链接："+downloadLink);
					final MDialog dialog=new MDialog(LogoView.this);
					dialog.setTitle("发现版本更新");
					StringBuilder sb=new StringBuilder();
					sb.append("版本号：").append(String.valueOf(AppTools.getVersionCode(mContext)))
						.append("->").append(String.valueOf(versionCode)).append("\n")
						.append(updateInfo);
					dialog.setMessage(sb.toString());
					dialog.setCancelable(false);
					dialog.setPositiveButton("更新", new OnClickListener(){
							@Override
							public void onClick(View p1) {
								Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(downloadLink));
								startActivity(intent);
							}
						});
					dialog.show();
					setInitMessage("版本更新");
				}});
	}

	@Override
	public void setInitMessage(final SpannableString str) {
		runOnUiThread(new Runnable(){public void run(){
				init_tv.setText(str);
		}});
	}
	
	@Override
	public void onClick(View p1) {
		switch(p1.getId()){
			case R.id.logo_skip:
				//TODO：跳过
				jumpView();
				break;
		}
	}
	
	/**
	 *@author BREEZE
	 *@description 跳转页面
	 */
	@Override
	public void jumpView() {
		UserSettings userSettings=UserSettings.getInstance(mContext);
		String phone=userSettings.getUserPhone();
		String pass=userSettings.getUserPass();
		if("".equals(phone)||"".equals(pass)){
			//TODO：跳转至登录界面
			isJumping=true;
			startActivity(new Intent(mContext,LoginView.class));
			finish();
		}else{
			//TODO：跳转至主页面
			isJumping=true;
			startActivity(new Intent(mContext,MainView.class));
			finish();
		}
	}

	/**
	*@author BREEZE
	*@description 停止使用
	*@param 原因
	*/
	@Override
	public void stopUsing(final String cause) {
		runOnUiThread(new Runnable(){public void run(){
			MDialog dialog=new MDialog(LogoView.this);
			dialog.setTitle("无法使用");
			dialog.setCancelable(false);
			dialog.setMessage(cause);
			dialog.setPositiveButton("退出", new OnClickListener(){
					@Override
					public void onClick(View p1) {
						finish();
					}
			});
			dialog.setNavigateBotton("加入QQ群", new OnClickListener(){
					@Override
					public void onClick(View p1) {
						if(AppTools.joinQQGroup(mContext)==false){
							MDialog.show(LogoView.this,"提示","未安装手Q或不支持！");
						}
					}
			});
			dialog.show();
			SpannableString str=new SpannableString("无法使用");
			ForegroundColorSpan fcs=new ForegroundColorSpan(Color.parseColor("#F44336"));
			str.setSpan(fcs,0,str.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
			setInitMessage(str);
		}});
	}

	/**
	*@author BREEZE
	*@description 设置初始化信息
	*@param 信息
	*/
	@Override
	public void setInitMessage(final String message) {
		runOnUiThread(new Runnable(){public void run(){
			init_tv.setText(message);
		}});
	}

	/**
	*@author BREEZE
	*@description 设置时间
	*@param 时间
	*/
	@Override
	public void setSkipTime(final int time) {
		runOnUiThread(new Runnable(){public void run(){
			if(time!=0){
				skip.setText(String.valueOf(time)+"｜"+"跳过");
			}else{
				skip.setText("跳过");
				if(!isJumping){
					jumpView();
				}
			}
		}});
	}

	@Override
	public void setBackground(final Bitmap result) {
		runOnUiThread(new Runnable(){public void run(){
			if(result!=null){
				background.setImageBitmap(result);
				SpannableString str=new SpannableString("欢迎使用");
				ForegroundColorSpan fc=new ForegroundColorSpan(Color.parseColor("#009688"));
				String welcome="欢迎使用";
				int start=welcome.indexOf("欢迎");
				int end=welcome.lastIndexOf("欢迎")+2;
				str.setSpan(fc,start,end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				setInitMessage(str);
				progressbar.setVisibility(View.GONE);
				//开始倒计时
				presenter.startSkipTime();
				skip.setVisibility(View.VISIBLE);
			}
		}});
	}
	
	private ImageView background;
	private TextView skip;
	private TextView init_tv;
	private ILogoPresenter presenter;
	private boolean isJumping=false;
	private ProgressBar progressbar;

	@Override
	public void init() {
		//获取控件
		background=f(R.id.logo_imageview);
		progressbar=f(R.id.logo_ProgressBar1);
		skip=f(R.id.logo_skip);
		init_tv=f(R.id.logo_init_tv);
	}

	@Override
	public void initData() {
		presenter=new LogoPresenter(this);
		//获取图片
		presenter.setBackground();
		//设置跳过背景
		skip.setBackground(GradientDrawableUtil.getLogoSkipDrawable());
		//设置跳过点击事件
		skip.setOnClickListener(this);
		//签名验证
		presenter.checkSignatures(this);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//界面全屏
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(getView(R.layout.view_logo));
		init();
		initData();
	}
}
